package com.neusoft.neuiotms.dcom.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.dto.MaintainRuleDTO;
import com.neusoft.neuiotms.dcom.model.enums.MaintainRuleStatus;
import com.neusoft.neuiotms.dcom.service.MaintainRuleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "维护规则管理")
@RestController
@RequestMapping(value = "/api/w/maintainRule")
public class MaintainRuleController {
    @Autowired
    private MaintainRuleService maintainRuleService;

    @ApiOperation("查询所有维护规则")
    @GetMapping
    public ResultListDTO<MaintainRuleDTO> index() {
        final List<MaintainRuleDTO> DTOs = this.maintainRuleService.findAll();
        final ResultListDTO<MaintainRuleDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation(value = "根据维护类别Code查询维护规则")
    @GetMapping("/listByModelCode/{code}")
    public ResultListDTO<MaintainRuleDTO> listByModelCode(@PathVariable final String code) {
        final List<MaintainRuleDTO> dtos = this.maintainRuleService.listByModelCode(code);
        final ResultListDTO<MaintainRuleDTO> result = ResultListDTO.success(dtos);
        return result;
    }

    @ApiOperation(value = "根据维护类别Code和规则状态查询维护规则")
    @GetMapping("/listByModelCodeAndStatus/{code}/{status}")
    public ResultListDTO<MaintainRuleDTO> listByModelCodeAndStatus(@PathVariable final String code,
            @PathVariable final String status) {
        final List<MaintainRuleDTO> dtos =
                this.maintainRuleService.listByModelCodeAndStatus(code, MaintainRuleStatus.valueOf(status));
        final ResultListDTO<MaintainRuleDTO> result = ResultListDTO.success(dtos);
        return result;
    }

    @ApiOperation("分页查询所有维护规则")
    @GetMapping("/p")
    public PageResultDTO<MaintainRuleDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.maintainRuleService.search(pageable, searchable));
    }

    @ApiOperation("创建维护规则")
    @PostMapping
    public ResultDTO<MaintainRuleDTO> create(final UserBean operator, @RequestBody final MaintainRuleDTO dto) {
        if (operator != null) {
            dto.setUserId(operator.getUserId());
            dto.setUserName(operator.getUsername());
        }
        final MaintainRuleDTO result = this.maintainRuleService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "修改维护规则")
    @PutMapping("/{id}")
    public ResultDTO<MaintainRuleDTO> update(@PathVariable final Long id,
            @RequestBody final MaintainRuleDTO dto) {
        final MaintainRuleDTO result = this.maintainRuleService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation(value = "变更维护规则状态")
    @PutMapping("/changeStatus/{id}/{flag}")
    public ResultDTO<MaintainRuleDTO> changeStatus(@PathVariable final Long id,
            @PathVariable final String flag) {
        final MaintainRuleDTO result = this.maintainRuleService.changeStatus(id, flag);
        return ResultDTO.success(result);
    }

    @ApiOperation("维护规则详情")
    @GetMapping("/{id}")
    public ResultDTO<MaintainRuleDTO> details(@PathVariable final Long id) {
        final MaintainRuleDTO dto = this.maintainRuleService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除维护规则")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.maintainRuleService.delete(id);
        return ResultDTO.success();
    }

}
